﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;
public partial class WebSite_m_paper_edit : System.Web.UI.Page
{
    public static int QuestionId = 0;
    public static string path="";
    public static string UserId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string userid;
        if (Session["UserId"] == null || Session["UserIdentity"].ToString() != "2")
        {
            Response.Redirect("Default.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
        else userid = Session["UserId"].ToString();

        try
        {
            if (!IsPostBack)
            {
                QuestionId = Convert.ToInt32(Request.QueryString["id"]);
                QuestionnaireHandler QHandler = new QuestionnaireHandler();
                DataSet ds = new DataSet();
                ds = QHandler.GetQuestionById(QuestionId);
                path = ds.Tables[0].Rows[0][4].ToString();
                UserId = ds.Tables[0].Rows[0][3].ToString();
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                XmlNode xn = doc.SelectSingleNode("/questionnaire/question/主题");
                this.TextBox1.Text = xn.InnerText;
                xn = doc.SelectSingleNode("/questionnaire/question/标题");
                this.TextBox2.Text = xn.InnerText;
                xn = doc.SelectSingleNode("/questionnaire/question/选项一");
                this.TextBox3.Text = xn.InnerText;
                xn = doc.SelectSingleNode("/questionnaire/question/选项二");
                this.TextBox4.Text = xn.InnerText;
                xn = doc.SelectSingleNode("/questionnaire/question/选项三");
                this.TextBox5.Text = xn.InnerText;
                xn = doc.SelectSingleNode("/questionnaire/question/选项四");
                this.TextBox6.Text = xn.InnerText;
            }
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message;
            Session["AtPage"] = "m_paper_modify.aspx";
            Response.Redirect("m_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds = new DataSet("questionnaire");
            ds.Tables.Add("question");
            ds.Tables["question"].Columns.Add("主题", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("Id", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("标题", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("选项一", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("选项二", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("选项三", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("选项四", Type.GetType("System.String"));
            DataRow newRow_1;
            newRow_1 = ds.Tables["question"].NewRow();
            newRow_1["主题"] = this.TextBox1.Text.ToString();
            newRow_1["标题"] = this.TextBox2.Text.ToString();
            newRow_1["选项一"] = this.TextBox3.Text.ToString();
            newRow_1["选项二"] = this.TextBox4.Text.ToString();
            newRow_1["选项三"] = this.TextBox5.Text.ToString();
            newRow_1["选项四"] = this.TextBox6.Text.ToString();
            QuestionnaireHandler Qhandler = new QuestionnaireHandler();
            newRow_1["Id"] = QuestionId.ToString();
            ds.Tables["question"].Rows.Add(newRow_1);
            string sXml = ds.GetXml();
            this.Label1.Text = QuestionId.ToString();


            StreamWriter sr = File.CreateText(path);
            sr.WriteLine(sXml);
            sr.Close();
            Qhandler.ModifyQuestionnaire(QuestionId, this.TextBox1.Text.ToString(), DateTime.Now, UserId, path);
            Response.Redirect("m_paper_check.aspx", false);
            return;
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message;
            Session["AtPage"] = "m_paper_modify.aspx";
            Response.Redirect("m_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
    }
}
